/**
 *
 * author ThuanLT
 * Aug 22, 2014
 *
 */

package vn.fsoft.gst.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import vn.fsoft.gst.domain.Directorate;
import vn.fsoft.gst.domain.Organisation;
import vn.fsoft.gst.domain.SupportingMaterial;
import vn.fsoft.gst.service.AddressService;
import vn.fsoft.gst.service.DirectorateService;
import vn.fsoft.gst.service.OrganisationService;
import vn.fsoft.gst.service.SupportingMaterialService;

@Controller
public class OrganisationController {

	@Autowired
	private OrganisationService organisationService;

	@Autowired
	private AddressService addressService;

	@Autowired
	private Organisation organisation;

	@Autowired
	private SupportingMaterialService suppService;
	
	@Autowired
	private DirectorateService directorateService;

	//created by ThanhLH to get all organisation
	@RequestMapping(value = "/listactiveorganisation", method = RequestMethod.GET)
	public String listOrganisations(ModelMap map) {
		map.addAttribute("organisation", new Organisation());
		map.addAttribute("listOrganisations",
				organisationService.getListOrganisations());
		return "listorganisations";
	}
	//create by ThanhLH to active in-active organisation
	@RequestMapping("/activeOragnisation.htm")
	public void activeOrganisation(HttpServletRequest request,
			HttpServletResponse response) {
		organisationService.activeOrganisation(Integer.parseInt(request
				.getParameter("orgID")));
	}

	// this method to load list Organisations create by ThanhLH
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String listOrg(ModelMap model) {
		model.addAttribute("organisation", new Organisation());
		model.addAttribute("listOrganisation",
				organisationService.getListOrganisations());
		return "listorg";
	}

	@RequestMapping(value = "/orgDetail", method = RequestMethod.GET)
	public String orgDetail(@RequestParam("orgId") int orgId, ModelMap modelMap) {
		
		modelMap.put("orgId", orgId);
		List<SupportingMaterial> list = suppService.getByOrg(orgId);
		Organisation organisation =organisationService.getOrganisationByID(orgId) ;
		List<Directorate> listDirectorates = directorateService.getListDirectorateByOrgID(orgId);
		
		System.err.println(listDirectorates.size());
		modelMap.addAttribute("organisation", organisation);
		modelMap.addAttribute("listSuppMaterial", list);
		modelMap.addAttribute("listDirectorate",listDirectorates);
		return "organisationDetails";
	}

	//create by ThuanLT to add an organisation object to model, to tranfer when add an Organisation
	@RequestMapping(value = "/addOrganisation", method = RequestMethod.GET)
	public String listOrgb(ModelMap model) {
		model.addAttribute("organisation", organisation);
		return "addOrganisation";
	}
	//create by ThuanLT to add a organisation into system, after add successfull, redirect to siste details of Organisation.
	@RequestMapping(value = "/addOrganisation", method = RequestMethod.POST)
	public String addOrgPost1(
			@ModelAttribute(value = "organisation") Organisation organisation,
			ModelMap model) {
		
		int result = organisationService.addOrganisation(organisation);
		System.out.println(result);
		return "redirect:orgDetail?orgId="+result;
	}
	//create by ThuanLT to mark an Organisation status to In-active
	@RequestMapping(value = "/inActiveOrganisation", method = RequestMethod.GET)
	public void inActiveOrganisation(@RequestParam(value="orgId") int orgId) {
		organisationService.inActiveOrganisation(orgId);
	}
	
	//create by ThuanLT to amend details of an Organisation.
	@RequestMapping(value = "/editOrganisation", method = RequestMethod.POST)
	public String editOrgPost(
			@ModelAttribute(value = "organisation") Organisation organisation,
			ModelMap model) {
		organisationService.updateOrganisation(organisation);
		
		return "redirect:orgDetail?orgId="+organisation.getOrgID();
	}
}
